package tests;


import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import tripleheap.InvalidSlotNumberException;
import tripleheap.InvalidTripleSizeException;
import tripleheap.THFBufMgrException;
import tripleheap.THFDiskMgrException;
import tripleheap.THFException;

import diskmgr.PCounter;
import diskmgr.Stream;
import basicpattern.BpJoin;
import global.AttrType;
import global.BPOrder;
import global.RID;
import global.SystemDefs;
import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;
import heap.Heapfile;
import heap.Scan;
import heap.Tuple;
import iterator.*;

public class BPJoinTest_old {
public static void main(String[] args) throws InvalidSlotNumberException, InvalidTripleSizeException, THFException, THFDiskMgrException, THFBufMgrException, Exception
{
	
//	String rdfdbname = args[0];
//	String SF1 = args[1];
//	String PF1 = args[2];
//	String OF1 = args[3];
//	double CF1 = Double.parseDouble(args[4]);
//	int iJNP = Integer.parseInt(args[5]);
//	int iJONO = Integer.parseInt(args[6]);
//	String iRSF = args[7];
//	String iRPF = args[8];
//	String iROF = args[9];
//	double iRCF = Double.parseDouble(args[10]);
//	String iLONP = args[11];
//	int iORS = Integer.parseInt(args[12]);
//	int iORO = Integer.parseInt(args[13]);
//	int oJNP = Integer.parseInt(args[14]);
//	int oJONO = Integer.parseInt(args[15]);
//	String oRSF = args[16];
//	String oRPF = args[17];
//	String oROF = args[18];
//	double oRCF = Double.parseDouble(args[19]);
//	String oLONP = args[20];
//	int oORS = Integer.parseInt(args[21]);
//	int oORO = Integer.parseInt(args[22]);
//	String SO = args[23];
//	String NP = args[24];
	
	String rdfdbname = "teradata";
// -----------Test query 1 -------
//		String SF1 = "dexxter";
//	String PF1 = "knows";
//	String OF1 = "Hannes_Gassert";
//	double CF1 = 0;
//	int iJNP = 3;
//	int iJONO = 0;
//	String iRSF = "Hannes_Gassert";
//	String iRPF = "nick";
//	String iROF = "hg";
//	double iRCF = 0;
//	String iLONP = "{1:3}";
//	int iORS = 0;
//	int iORO = 1;
//	int oJNP = 3;
//	int oJONO = 0;
//	String oRSF = "hg";
//	String oRPF = "name";
//	String oROF = "Hannes_Gassert";
//	double oRCF = 0;
//	String oLONP = "{1:2:3}";
//	int oORS = 0;
//	int oORO = 1;
//	int SO = 0;//Integer.parseInt(args[23]);
//	int NP = 4;//Integer.parseInt(args[24]);
//	int NumPages = 100;//Integer.parseInt(args[25]);
//	int NUMBUF = 100;//Integer.parseInt(args[26]);
	
	String SF1 = "urs";
	String PF1 = "knows";
	String OF1 = "0";
	double CF1 = 0;
	int iJNP = 3;
	int iJONO = 0;
	String iRSF = "0";
	String iRPF = "type";
	String iROF = "0";
	double iRCF = 0;
	String iLONP = "{1:3}";
	int iORS = 0;
	int iORO = 1;
	int oJNP = 1;
	int oJONO = 0;
	String oRSF = "urs";
	String oRPF = "0";
	String oROF = "0";
	double oRCF = 0;
	String oLONP = "{1:2:3}";
	int oORS = 0;
	int oORO = 1;
	int SO = 1;//Integer.parseInt(args[23]);
	int NP = 2;//Integer.parseInt(args[24]);
	int NumPages = 100;//Integer.parseInt(args[25]);
	int NUMBUF = 100;//Integer.parseInt(args[26]);
	
	
	int i=0;
	
	int[] iLONP1=null;
	//Scanner sc = new Scanner(iLONP).useDelimiter(":");
//	while(sc.hasNext())
//	{
//		iLONP1[i]= new Integer(sc.nextInt());
//		i++;
//	}
	iLONP1 = new int[2];
	iLONP1[0] = 1;
	iLONP1[1] = 3;
	//iLONP1[2] = 1;
	
	i=0;
	
	int []oLONP1 = null;
//	sc = new Scanner(oLONP).useDelimiter(":");
//	while(sc.hasNext()){
//	oLONP1[i]= new Integer(sc.nextInt());
//	i++;
//	}
	
	oLONP1 = new int[3];
	oLONP1[0] = 1;
	oLONP1[1] = 2;
	oLONP1[2] = 3;
	//oLONP1[2] = 1;
	//int NumPages = Integer.parseInt(args[25]);
	//int NUMBUF = Integer.parseInt(args[26]);
	
	BpJoin bpj2 = null ; 
	BpJoin bpj1 = null ;
	//BPIterator bpit=null;

	File file = new File("teradata");
	boolean exists = file.exists();
	if (exists) {
		SystemDefs.MINIBASE_RESTART_FLAG=false;
		SystemDefs sysdef = new SystemDefs("teradata", 0, 5500, "Clock");
	} else {
		System.out.println("DB does not exits");
	}
	Stream stream = SystemDefs.JavabaseDB.openStream("teradata", 0, SF1, PF1, OF1, CF1, NUMBUF);
	Iterator left_lt = (Iterator)stream;
	BPIterator bpit = (BPIterator)left_lt;
	Heapfile hf = new Heapfile("Madarchod"); 
	Tuple FTuple,JTuple;
	
	
	/*
	 * int,int,BPIterator,int,int,String,String,String,double,int [],int,int)
	 */
	
	try {
		bpj2 = new BpJoin (100, 3, bpit, iJNP, iJONO, iRSF, iRPF, iROF, iRCF,iLONP1, iORS,iORO );
		//while(true){
		//JTuple = bpj2.get_next();
		//if(JTuple == null)
		//	break;
		
		bpj1 = new BpJoin (100, 3, (iterator.BPIterator) bpj2, oJNP, oJONO, oRSF, oRPF, oROF, oRCF,oLONP1, oORS, oORO);
		
		int totalNodes = oLONP1.length;
		if(oORS == 1)
			totalNodes++;
		if(oORO == 1)
			totalNodes++;
		totalNodes++;
		
		AttrType[] attrType = new AttrType[totalNodes];
		for(int l=0;l<totalNodes-1;l++)
			attrType[l] = new AttrType(AttrType.attrString);
		 
		 attrType[totalNodes-1] = new AttrType(AttrType.attrReal);
		 
		short[] strSize = new short[totalNodes-1];
		for(int m=0;m<totalNodes-1;m++)
			strSize[m] = 100;
		
		BPOrder order = new BPOrder(SO);
		BPSort bpSort = new BPSort(attrType, (short)totalNodes, strSize,(iterator.BPIterator)bpj1, order, NP, NumPages );
		while(true){
			FTuple = bpSort.get_next();
			if(FTuple == null) break;
			//hf.insertRecord(FTuple.getTupleByteArray());	
			FTuple.setHdr((short)totalNodes, attrType, strSize);
			//String[] str = new String[totalNodes-1];
//			for(int j=0;j<str.length;j++)
//				str[j] = FTuple.getStrFld(j+1);
			
			
			for(int j=0;j<totalNodes-1;j++)
				System.out.print(FTuple.getStrFld(j+1) + "\t\t");
			float conf = FTuple.getFloFld(totalNodes);
			System.out.println(conf + "\n");
		}
		bpSort.close();
		stream.close();
		//}
	}
	catch (Exception e) {

		System.err.println ("*** Error preparing for nested_loop_join");
		System.err.println (""+e);
		Runtime.getRuntime().exit(1);
	}
	
//	AttrType[] attrType = new AttrType[5];
//	attrType[0] = new AttrType(AttrType.attrString);
//	 attrType[1] = new AttrType(AttrType.attrString);
//	 attrType[2] = new AttrType(AttrType.attrString);
//	 attrType[3] = new AttrType(AttrType.attrString);
//	 attrType[4] = new AttrType(AttrType.attrReal);
//	 
//	 short[] strSize = new short[4];
//	 strSize[0] = 100;
//	 strSize[1] = 100;
//	 strSize[2] = 100;
//	 strSize[3] = 100;
//	 
//	Scan tmpScan = hf.openScan();
//	RID ridtmp = new RID();
//	Tuple tmpTuple = tmpScan.getNext(ridtmp);
//	while(tmpTuple != null)
//	{
//		tmpTuple.setHdr((short)5, attrType, strSize);
//		String str1 = tmpTuple.getStrFld(1);
//		String str2 = tmpTuple.getStrFld(2);
//		String str3 = tmpTuple.getStrFld(3);
//		String str4 = tmpTuple.getStrFld(4);
//		//double conf = tmpTuple.getDblFld(4);
//		float conf = tmpTuple.getFloFld(5);
//		System.out.println(str1 + "\t\t"+ str2 +"\t\t"+ str3 + "\t\t"+ str4 + "\t\t" + conf);
//		tmpTuple = tmpScan.getNext(ridtmp);
//	}
//	tmpScan.closescan();
//	hf.deleteFile();
	System.out.println("No of reads : "+PCounter.rcounter+"\nNo of writes : "+PCounter.wcounter);
	SystemDefs.JavabaseDB.closeDB();
}
}
	
	
	
	
	/*
	Stream stream = SystemDefs.JavabaseDB.openStream(SystemDefs.MINIBASE_DBNAME, 0, RightSubjectFilter, RightPredicateFilter, RightObjectFilter, RightConfidenceFilter, amt_of_mem);
	Iterator left_lt = (Iterator)stream;
	BPIterator bpit = (BPIterator)left_lt;
	Heapfile hf = new Heapfile("Madarchod"); 
	Tuple FTuple,JTuple;
	
	
	
	joinNodePosition;
	JoinOnSubjectorObject;
	try {
		bpj2 = new BpJoin (100, 4, Jsizes, bpit, joinNodePosition, JoinOnSubjectorObject, RightSubjectFilter, RightPredicateFilter, RightObjectFilter, RightConfidenceFilter
				LeftOutNodePositions, OutputRightSubject, OutputRightObject);
		while(true){
		JTuple = bpj2.get_next();
		if(JTuple == null)
			break;
		
		bpj1 = new BpJoin (100, 4, Jsizes, (iterator.BPIterator) bpj2, joinNodePosition1, JoinOnSubjectorObject1, RightSubjectFilter1, RightPredicateFilter1, RightObjectFilter1, RightConfidenceFilter1
				LeftOutNodePositions1, OutputRightSubject1, OutputRightObject1);
		
		while(true){
			FTuple = bpj1.get_next();
			if(FTuple == null) break;
			hf.insertRecord(FTuple.getTupleByteArray());	
		}
		}
	}
	catch (Exception e) {
		System.err.println ("*** Error preparing for nested_loop_join");
		System.err.println (""+e);
		Runtime.getRuntime().exit(1);
	}
}
}
*/